package com.hepinghospital.user.ui.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseDialogFragment;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.AreaResponse;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2017/11/23.
 */

public class AreaDialogFragment extends BaseDialogFragment {

    public static final int INDEX_TAB_PROVINCE = 0;
    public static final int INDEX_TAB_CITY = 1;
    public static final int INDEX_TAB_AREA = 2;

    public static final int INDEX_INVALID = -1;

    public static AreaDialogFragment newInstance() {
        return new AreaDialogFragment();
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView tv_province, tv_city, tv_area;
    private View tab_province, tab_city, tab_area;
    private String province, city, area;
    private String provinceid, cityid, areaid;
    private MyAdapter myAdapter;
    private List<AreaResponse> mList = new ArrayList<>();
    private List<AreaResponse> list_province = new ArrayList<>();
    private List<AreaResponse> list_city = new ArrayList<>();
    private List<AreaResponse> list_area = new ArrayList<>();

    private String colorSelected = "#333333";
    private String colorNomal = "#209187";
    private int tabIndex = INDEX_TAB_PROVINCE;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_area, container);

        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        myAdapter = new MyAdapter(mList);
        recyclerView.setAdapter(myAdapter);

        ImageView btn_confirm = view.findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(v -> dismiss());

        tv_province = view.findViewById(R.id.tv_province);
        tv_city = view.findViewById(R.id.tv_city);
        tv_area = view.findViewById(R.id.tv_area);
        tv_province.setOnClickListener(v -> {
            tabIndex = INDEX_TAB_PROVINCE;
            if (list_province.size() > 0) {
                mList.clear();
                mList.addAll(list_province);
                myAdapter.notifyDataSetChanged();
            }
            updateTabsStyle(tabIndex);
        });

        tv_city.setOnClickListener(v -> {
            tabIndex = INDEX_TAB_CITY;
            if (list_city.size() > 0) {
                mList.clear();
                mList.addAll(list_city);
                myAdapter.notifyDataSetChanged();
            }
            updateTabsStyle(tabIndex);
        });

        tv_area.setOnClickListener(v -> {
            tabIndex = INDEX_TAB_AREA;
            if (list_area.size() > 0) {
                mList.clear();
                mList.addAll(list_area);
                myAdapter.notifyDataSetChanged();
            }
            updateTabsStyle(tabIndex);
        });

        tab_province = view.findViewById(R.id.tab_province);
        tab_city = view.findViewById(R.id.tab_city);
        tab_area = view.findViewById(R.id.tab_area);

        updateTabsStyle(tabIndex);
        getProvince();
        return view;
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
//        Objects.requireNonNull(Objects.requireNonNull(getDialog()).getWindow()).requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        Objects.requireNonNull(getDialog().getWindow()).setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }


    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String ids, String address);
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {
        private int selectIndex = -1;
        List<AreaResponse> list;

        MyAdapter(List<AreaResponse> list) {
            this.list = list;
        }

        @NotNull
        @Override
        public Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            return new Holder(getLayoutInflater().inflate(R.layout.item_area, parent, false));
        }

        @Override
        public void onBindViewHolder(final Holder holder, final int position) {
            holder.tv_name.setText(list.get(position).name);
            holder.itemView.setOnClickListener(v -> selectedList(position));
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public int getSelectIndex() {
            return selectIndex;
        }

        public void setSelectIndex(int position) {
            this.selectIndex = position;
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv_name;

            Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
            }
        }
    }


    private void getProvince() {
        RestProxy.getInstance().getProvince(new BaseObserver<List<AreaResponse>>() {
            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(getActivity(), e);
            }

            @Override
            public void onSuccess(List<AreaResponse> areaResponse) {
                list_province.clear();
                list_province.addAll(areaResponse);
                mList.clear();
                mList.addAll(list_province);
                myAdapter.notifyDataSetChanged();
            }
        });
    }


    private void getArea(String address_id) {
        RestProxy.getInstance().getArea(address_id, new BaseObserver<List<AreaResponse>>() {
            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(getActivity(), e);
            }

            @Override
            public void onSuccess(List<AreaResponse> areaResponse) {
                list_area.clear();
                list_area.addAll(areaResponse);
                mList.clear();
                mList.addAll(list_area);
                myAdapter.notifyDataSetChanged();
            }
        });
    }

    private void getCity(String address_id) {
        RestProxy.getInstance().getCity(address_id, new BaseObserver<List<AreaResponse>>() {
            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(getActivity(), e);
            }

            @Override
            public void onSuccess(List<AreaResponse> areaResponse) {
                list_city.clear();
                list_city.addAll(areaResponse);
                mList.clear();
                mList.addAll(list_city);
                myAdapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 设置选中的城市tab是否可见
     */
    private void updateTabsStyle(int tabIndex) {
        switch (tabIndex) {
            case INDEX_INVALID:
            case INDEX_TAB_PROVINCE:
                tv_province.setTextColor(Color.parseColor(colorNomal));
                tv_province.setVisibility(View.VISIBLE);
                tv_province.setText("请选择");
                tab_province.setVisibility(View.VISIBLE);
                tv_city.setVisibility(View.GONE);
                tab_city.setVisibility(View.GONE);
                tv_area.setVisibility(View.GONE);
                tab_area.setVisibility(View.GONE);
                break;

            case INDEX_TAB_CITY:
                tv_province.setTextColor(Color.parseColor(colorSelected));
                tv_province.setVisibility(View.VISIBLE);
                tab_province.setVisibility(View.GONE);
                tv_city.setTextColor(Color.parseColor(colorNomal));
                tv_city.setVisibility(View.VISIBLE);
                tv_city.setText("请选择");
                tab_city.setVisibility(View.VISIBLE);
                tv_area.setVisibility(View.GONE);
                break;

            case INDEX_TAB_AREA:
                tv_province.setTextColor(Color.parseColor(colorSelected));
                tv_city.setTextColor(Color.parseColor(colorSelected));
                tv_area.setTextColor(Color.parseColor(colorNomal));
                tv_province.setVisibility(View.VISIBLE);
                tab_province.setVisibility(View.GONE);
                tab_city.setVisibility(View.GONE);
                tv_city.setVisibility(View.VISIBLE);
                tv_area.setVisibility(View.VISIBLE);
                tab_area.setVisibility(View.VISIBLE);
                tv_area.setText("请选择");
                break;
            default:
                break;
        }
    }

    private void selectedList(int position) {
        switch (tabIndex) {
            case INDEX_TAB_PROVINCE:
                province = mList.get(position).name;
                provinceid = mList.get(position).id;
                tv_province.setText(mList.get(position).name);
                getCity(mList.get(position).id);
                tabIndex = INDEX_TAB_CITY;
                updateTabsStyle(tabIndex);
                break;

            case INDEX_TAB_CITY:
                city = mList.get(position).name;
                cityid = mList.get(position).id;
                tv_city.setText(mList.get(position).name);
                getArea(mList.get(position).id);
                tabIndex = INDEX_TAB_AREA;
                updateTabsStyle(tabIndex);
                break;

            case INDEX_TAB_AREA:
                area = mList.get(position).name;
                areaid = mList.get(position).id;
                tv_area.setText(mList.get(position).name);
                mListener.onConfirm(provinceid + "," + cityid + "," + areaid, province + "   " + city + "   " + area);
                tabIndex = INDEX_TAB_PROVINCE;
                dismiss();
                break;
            default:
                break;
        }
    }
}
